﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUSSINESS;

namespace QuanLyGiaPha
{
    public partial class frmLogin : Form
    {
        private BUS_NguoiDung busNguoiDung = new BUS_NguoiDung();
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                string username = txtUsername.Text;
                string password = txtPassword.Text;
                DataTable tbNguoiDung = busNguoiDung.Login(username, password);
                if (tbNguoiDung != null && tbNguoiDung.Rows.Count > 0)
                {
                    DTO_NguoiDung.isAdministrator = Convert.ToBoolean(tbNguoiDung.Rows[0][DTO_NguoiDung.ISADMIN]);
                    frmMain frm =  new frmMain();
                    frm.Show();
                    frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Tên đăng nhập hoặc mật khẩu không đúng!!!", "Thất bại", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {

        }

        private void txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(sender, null);
            }
        }
    }
}
